package com.june.junemplayer.mapper;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

import com.june.junemplayer.pojo.SongList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class SongListMapper {

    public void insert(Context context, SongList songList) {
        Log.d("JuneSQLite", "----SongList----insert() start:songList = " + songList + "----");
        SongListSQLiteOpenHelper songListSQLiteOpenHelper = new SongListSQLiteOpenHelper(context, "MusicPlayerDatabase.db", null, SongListSQLiteOpenHelper.getVersion());
        SQLiteDatabase database = songListSQLiteOpenHelper.getWritableDatabase();
        ContentValues value = new ContentValues();
        value.put("title", songList.getTitle());
        value.put("owner", songList.getOwner());
        value.put("description", songList.getDescription());
        value.put("cover", songList.getCover());
        value.put("number", songList.getNumber());
        value.put("frequency", songList.getFrequency());
        value.put("visible", songList.isVisible() ? 1 : 0);
        value.put("extra", songList.getExtra());
        Log.d("JuneSQLiteInfo", "----SongList----插入----" + value);
        database.insert("SongList", null, value);
        Log.d("JuneSQLite", "----SongList----insert() end:value = " + value + "----");
    }

    public int delete(Context context, String title) {
        Log.d("JuneSQLite", "----SongList----delete() start:title = " + title + "----");
        SongListSQLiteOpenHelper songListSQLiteOpenHelper = new SongListSQLiteOpenHelper(context, "MusicPlayerDatabase.db", null, SongListSQLiteOpenHelper.getVersion());
        SQLiteDatabase database = songListSQLiteOpenHelper.getWritableDatabase();
        String[] titles = new String[]{title};
        Log.d("JuneSQLite", "----SongList----delete() end----");
        return database.delete("SongList", "title = ? ", titles);
    }

    public void update(Context context, SongList songList) {
        Log.d("JuneSQLite", "----SongList----update() start:songList = " + songList + "----");
        if (delete(context, songList.getTitle()) == -1) {
            Log.e("JuneSQLiteFalse", "----SongList----delete() false----");
            Log.e("JuneSQLiteFalse", "-----SongList-----update() false-----");
            return;
        }
        insert(context, songList);
        Log.d("JuneSQLite", "----SongList----update() end----");
    }

    @SuppressLint("Range")
    public ArrayList<SongList> retrieveAllSongLists(Context context) {
        Log.d("JuneSQLite", "----SongList----retrieveAllSongLists() start----");
        SongListSQLiteOpenHelper songListSQLiteOpenHelper = new SongListSQLiteOpenHelper(context,
                "MusicPlayerDatabase.db", null, SongListSQLiteOpenHelper.getVersion());
        SQLiteDatabase database = songListSQLiteOpenHelper.getWritableDatabase();
        Cursor cursor = database.query("SongList", null, null,
                null, null, null, null);
        ArrayList<SongList> songLists = new ArrayList<>();
        int count = 0;
        if (cursor.moveToFirst()) {
            do {
                SongList songList = new SongList();
                songList.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                songList.setOwner(cursor.getLong(cursor.getColumnIndex("owner")));
                songList.setDescription(cursor.getString(cursor.getColumnIndex("description")));
                songList.setCover(cursor.getString(cursor.getColumnIndex("cover")));
                songList.setNumber(cursor.getInt(cursor.getColumnIndex("number")));
                songList.setFrequency(cursor.getInt(cursor.getColumnIndex("frequency")));
                songList.setVisible(cursor.getInt(cursor.getColumnIndex("visible")) == 1);
                songList.setExtra(cursor.getString(cursor.getColumnIndex("extra")));
                Log.d("JuneSQLiteInfo", count + "----" + songList);
                count++;
                songLists.add(songList);
            } while (cursor.moveToNext());
        }
        cursor.close();
        Log.d("JuneSQLite", "----SongList----retrieveAllSongLists() end:count = "+count+"----");
        return songLists;
    }

    public static class SongListSQLiteOpenHelper extends SQLiteOpenHelper {

        private static int mVersion = 1;
        String create = "create table SongList(title text primary key,owner integer,path text,description text,cover text,number integer,frequency integer,visible integer,extra text)";

        public SongListSQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
            mVersion = version == 0 ? 1 : version;
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL(create);
            Log.d("JuneSQLite", "创建成功----SongList----版本" + sqLiteDatabase.getVersion());
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
//            sqLiteDatabase.execSQL(create);
            Log.d("JuneSQLite", "更新成功----SongList----版本" + oldVersion + "  ->  " + newVersion);
            mVersion = newVersion;
        }

        public static int getVersion() {
            return mVersion;
        }
    }
}